﻿app.controller('ActivityEnterCtrl', ['$scope', '$http', '$stateParams', '$window', '$filter', 'utils', 'httpPlus', '$state', function ($scope, $http, $stateParams, $window, $filter, utils, httpPlus, $state) {
    utils.setTitle('活动报名');
    $scope.currStore = undefined;
    $scope.currTime = undefined;
    self.tips = undefined;       
    $scope.enrollActivity_click = function () {
        $scope.tips = undefined;
        if ($scope.name == undefined || $scope.name == null) {
            return $scope.tips = '姓名不能为空!'
        }
        if ($scope.tel == undefined || $scope.name == null || /^1[34578]\d{9}$/.test($scope.tel) != true) {
            return $scope.tips = '手机号不能为空或格式不正确!'
        }
        if (_.includes($scope.contactInfos, 'Number') && ($scope.count == undefined || $scope.count == null || /^[1-9]\d*$|^0$/.test(String($scope.count)) != true)) {
                return $scope.tips = '人数不能为空或格式不正确!'         
        }
        if ($scope.currStore == undefined) {
            return $scope.tips = '餐厅不能为空!'
        }
        if ($scope.currTime == undefined) {
            return $scope.tips = '场次不能为空!'
        }

            httpPlus
                .MMB2RRRApi('MyMemberEventRegistration/EventRegister', {
                    _info: {
                        EventGUID: $stateParams.id,
                        Contact_Name: $scope.name,
                        Contact_Phone: $scope.tel,
                        Contact_Number: $scope.count,
                        PlayTimespan: $scope.currTime,
                        ShopCode: $scope.currStore.id
                    }
                })
                .then(function (data) {
                    $state.go('wx.pay_result', { id: data.MyEventGUID })
                })
    };
    httpPlus
            .MMB2RRRApi('Manage/MemberEventRegistration/GetByGUID', {
                _guid: $stateParams.id
            })
            .then(function (data) {
                console.log(data);
                $scope.title = data.Title;
                $scope.time = data.StartTime.substring(0, 10) + '-' + data.EndTime.substring(0, 10);
                $scope.price = data.EntryFee/100;
                $scope.storeList = _.map(data.Shops, function (val) {
                    return {
                        id: val.Code,
                        name: val.Name
                    }
                });
                $scope.timeList = _.map(data.PlayTimespans, function (val) {
                    return val = val.substring(0, 4) + '/' + val.substring(4, 6) + '/' + val.substring(6, 8) + ' ' + val.substring(8, 10) + ':' + val.substring(10, 12) + ' - ' + val.substring(12, 16) + '/' + val.substring(16, 18) + '/' + val.substring(18, 20) + ' ' + val.substring(20, 22) + ':' + val.substring(22, 24);
                });
                $scope.contactInfos = data.ContactInfos;
                $scope.currStore = $scope.storeList[0];
                $scope.currTime = $scope.timeList[0];
            })
        .catch(function(err){
            alert(err.ResultMsg);
        });
}])